﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;

namespace Firefly.Forms {
    public partial class WorldSelectorForm : Form {
        public WorldSelectorForm() {
            InitializeComponent();
            LoadAvailableWorlds();
        }

        /// <summary>
        /// THIS FORM IS NOT ALLOWED TO DISPOSE. STOP TRYING, DAMMIT.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosing(CancelEventArgs e) {
            e.Cancel = true;
            this.Hide();
        }

        public void LoadAvailableWorlds() {
            string[] worldNames = Data.WorldFile.GetWorldList();
            dataGridView1.RowCount = 0;

            for (int i = 0; i < worldNames.Length; i++) {
                Data.WorldConfig wc = Data.WorldFile.Load(worldNames[i]);

                dataGridView1.Rows.Add(new DataGridViewRow());
                DataGridViewTextBoxCell worldName = (DataGridViewTextBoxCell)(dataGridView1.Rows[i].Cells[0]);
                DataGridViewComboBoxCell ComboColumn = (DataGridViewComboBoxCell)(dataGridView1.Rows[i].Cells[1]);
                DataGridViewTextBoxCell serverAddress = (DataGridViewTextBoxCell)(dataGridView1.Rows[i].Cells[2]);
                DataGridViewTextBoxCell serverPort = (DataGridViewTextBoxCell)(dataGridView1.Rows[i].Cells[3]);
                worldName.Value = wc.WorldName;
                serverAddress.Value = wc.ServerAddress;
                serverPort.Value = wc.ServerPort.ToString();

                string[] characterNames = new string[wc.Characters.Count];
                int dCharacter = 0;
                /*foreach(Character c in wc.Characters) {
                    characterNames[cIndex] = c.Name;
                    if (c.IsDefault)
                        dCharacter = cIndex;
                    cIndex++;
                }*/
                for (int c = 0; c < wc.Characters.Count; c++) {
                    characterNames[c] = wc.Characters[c].Name;
                    if (wc.Characters[c].IsDefault)
                        dCharacter = c;
                }
                Console.WriteLine("Default Char: " + dCharacter);
                ComboColumn.DataSource = (characterNames.Length > 0) ? characterNames : new string[] { "None" };
                ComboColumn.Value = (characterNames.Length > 0) ? characterNames[dCharacter] : "None";
                
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
            if (e.RowIndex > -1) {
                // Open the selected world.
                Console.WriteLine("Clicked Cell: " + dataGridView1.Rows[e.RowIndex].Cells[0]);
                ClientForm cf = (ClientForm)Program.WindowManager.Forms["Client"];
                cf.WorldCache.Add(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                this.Close();
            }
        }

        private void buttonAddWorld_Click(object sender, EventArgs e) {
            Program.WindowManager.OpenForm("World Editor");
            WorldEditorForm wef = (WorldEditorForm)Program.WindowManager.Forms["World Editor"];
        }

        private void buttonEditWorld_Click(object sender, EventArgs e) {
            if (dataGridView1.Rows.Count != 0) {
                Program.WindowManager.OpenForm("World Editor");
                WorldEditorForm wef = (WorldEditorForm)Program.WindowManager.Forms["World Editor"];
                wef.OpenWorld((string)dataGridView1.SelectedRows[0].Cells[0].Value);
            }
        }

        private void buttonDeleteWorld_Click(object sender, EventArgs e) {
            if (dataGridView1.Rows.Count != 0) {
                if (MessageBox.Show("Are you sure you wish to delete " + (string)dataGridView1.SelectedRows[0].Cells[0].Value 
                    + "?\nThis action is not reversible.", "World Deletion Confirmation", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Warning) == DialogResult.Yes) {
                    // User wishes to delete this world.
                        Data.WorldFile.Delete((string)dataGridView1.SelectedRows[0].Cells[0].Value);
                        LoadAvailableWorlds();
                }
            }
        }

        private void buttonAddPlayer_Click(object sender, EventArgs e) {
            if (dataGridView1.Rows.Count != 0) {
                WorldEditorForm wef = (WorldEditorForm)Program.WindowManager.Forms["World Editor"];
            }
        }

        private void buttonEditPlayer_Click(object sender, EventArgs e) {
            if (dataGridView1.Rows.Count != 0) {
                WorldEditorForm wef = (WorldEditorForm)Program.WindowManager.Forms["World Editor"];
            }
        }

        private void buttonConnect_Click(object sender, EventArgs e) {
            if (dataGridView1.Rows.Count != 0) {
                ClientForm cf = (ClientForm)Program.WindowManager.Forms["Client"];
                cf.WorldCache.Add((string)dataGridView1.SelectedRows[0].Cells[0].Value);
                this.Close();
            }
        }
    }
}
